package com.ews.onlineexchange.controller.assess;

import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import com.ews.onlineexchange.model.assess.Dict;
import com.ews.onlineexchange.service.EntityHome;
import com.ews.onlineexchange.service.assess.DictService;

@Named
@ConversationScoped
public class DictHome extends EntityHome<Dict, String> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Inject
	DictService dictService;
	private String tempTitle;
	
	@Override
	public Dict loadInstance() {
		Dict instance = super.loadInstance();
		setTempTitle(instance.getTitle());
		return instance;
	}


	@Override
	public String saveAndFlush() {
		String currTitle = getInstance().getTitle();
		if(!currTitle.equals(tempTitle)) {//新增时判断
			if(dictService.findByTitle(currTitle)) {
				FacesContext.getCurrentInstance().addMessage("title",
						new FacesMessage(FacesMessage.SEVERITY_ERROR, "评价事项已存在！", ""));
				return null;
			}
		}
		String result = super.saveAndFlush();
		if(result.equals(RESULT_SAVED))
		return "DictList.xhtml?faces-redirect=true&amp;includeViewParams=true";
		else return null;
	}


	public String getTempTitle() {
		return tempTitle;
	}


	public void setTempTitle(String tempTitle) {
		this.tempTitle = tempTitle;
	}


	
}
